package ynu.edu.service;
import ynu.edu.dao.IUserDao;
import ynu.edu.po.DeleteUserRequest;
import ynu.edu.po.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import ynu.edu.po.UserRegistrationRequest;


@Api(tags = "用户管理相关接口")
@RestController
@RequestMapping("/api/User")
public class UserApi {
    @Autowired
    private IUserDao userDao;

    @GetMapping("/login/{id}/{password}")
    @ApiOperation("用户登录的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true),
            @ApiImplicitParam(name = "password", value = "用户密码", required = true)
    })
    public String login(@PathVariable String id, @PathVariable String password) {
        User u = userDao.findUserByIdAndPassword(id, password);
        if (u == null) {
            return "Please Login in first!";
        }
        return "Welcome " + u.getName() + "!";
    }

    @PostMapping("/login2")
    @ApiOperation("用户登录的接口 (使用请求体)")
    public ResponseEntity<?> loginByIdAndPassword(@RequestBody User user) {
        User u = userDao.findUserByIdAndPassword(user.getId(), user.getPassword());
        if (u == null) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid username or password");
        }
        return ResponseEntity.ok(u);
    }


    @PostMapping("/Register")
    @ApiOperation("用户注册的接口")
    public String registerUser(@RequestBody UserRegistrationRequest userRequest) {
        String id = userRequest.getId();
        if (id == null || id.isEmpty()) {
            return "Please complete the id";
        }

        User u = new User();
        u.setStuId(userRequest.getStuId());
        u.setId(id);
        u.setName(userRequest.getName());
        u.setPassword(userRequest.getPassword());
        u.setSex(userRequest.isSex());
        u.setAge(userRequest.getAge());

        try {
            userDao.save(u);
        } catch (Exception e) {
            return e.getMessage();
        }
        return "User created successfully!";
    }


    @GetMapping("/findAll")
    @ApiOperation("获取所有用户")
    public List<User> findAll() {
        List<User> userList = userDao.findAll();
        return userList;
    }


    @PutMapping("/update")
    @ApiOperation("更新用户信息")
    public String update(@RequestBody User user) {
        try {
            userDao.save(user);
        } catch (Exception e) {
            return e.getMessage();
        }
        return "User updated successfully!";
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除用户")
    public String deleteUser(@RequestBody DeleteUserRequest request) {
        try {
            userDao.deleteById(request.getId());
        } catch (Exception e) {
            return "Error deleting user: " + e.getMessage();
        }
        return "User deleted successfully!";
    }

}
